package com.hohd;

import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.google.gson.Gson;
import com.kh.first.pojo.common.AjaxJson;
import lombok.Data;
import lombok.ToString;

import javax.naming.Name;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class T0319Json {
    public static void main(String[] args) throws IOException {
        new T0319Json().toJson();
//        test01();
//        test02();
//        test03();
    }

    private static void test03() throws IOException {
        String json = "{\n" +
                "  \"errcode\": 0,\n" +
                "  \"errmsg\": \"ok\",\n" +
                "  \"userlist\": [\n" +
                "    {\n" +
                "      \"userid\": \"zhangyurong\",\n" +
                "      \"name\": \"张玉荣\",\n" +
                "      \"department\": [\n" +
                "        127,\n" +
                "        129\n" +
                "      ],\n" +
                "      \"position\": \"科长\",\n" +
                "      \"mobile\": \"13813535407\",\n" +
                "      \"gender\": \"1\",\n" +
                "      \"email\": \"zhangyurong@khmm.com.cn\",\n" +
                "      \"avatar\": \"http://wework.qpic.cn/bizmail/exhRGGCSJhRaPACXkgTtxy437nXTJfZRs0cxnZTmxWuLGCsln4N1cQ/0\",\n" +
                "      \"status\": 1,\n" +
                "      \"enable\": 1,\n" +
                "      \"isleader\": 1,\n" +
                "      \"extattr\": {\n" +
                "        \"attrs\": []\n" +
                "      },\n" +
                "      \"hide_mobile\": 0,\n" +
                "      \"english_name\": \"20100621\",\n" +
                "      \"telephone\": \"\",\n" +
                "      \"order\": [\n" +
                "        0,\n" +
                "        0\n" +
                "      ],\n" +
                "      \"external_profile\": {\n" +
                "        \"external_attr\": [],\n" +
                "        \"external_corp_name\": \"\"\n" +
                "      },\n" +
                "      \"qr_code\": \"https://open.work.weixin.qq.com/wwopen/userQRCode?vcode=vc022fd756e0280121\",\n" +
                "      \"alias\": \"20100621\",\n" +
                "      \"is_leader_in_dept\": [\n" +
                "        0,\n" +
                "        1\n" +
                "      ],\n" +
                "      \"address\": \"\",\n" +
                "      \"thumb_avatar\": \"http://wework.qpic.cn/bizmail/exhRGGCSJhRaPACXkgTtxy437nXTJfZRs0cxnZTmxWuLGCsln4N1cQ/100\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"userid\": \"qianjiaqi\",\n" +
                "      \"name\": \"钱嘉琪\",\n" +
                "      \"department\": [\n" +
                "        129\n" +
                "      ],\n" +
                "      \"position\": \"\",\n" +
                "      \"mobile\": \"18915882686\",\n" +
                "      \"gender\": \"1\",\n" +
                "      \"email\": \"qianjiaqi@khmm.com.cn\",\n" +
                "      \"avatar\": \"http://wework.qpic.cn/wwhead/duc2TvpEgST9hicuyypLEKEwxplgFSRsHibBQUuibGIZJPkCCxWaqM2QtDMlTl8TUia0shzS7DB7n50/0\",\n" +
                "      \"status\": 1,\n" +
                "      \"enable\": 1,\n" +
                "      \"isleader\": 0,\n" +
                "      \"extattr\": {\n" +
                "        \"attrs\": []\n" +
                "      },\n" +
                "      \"hide_mobile\": 0,\n" +
                "      \"english_name\": \"20140392\",\n" +
                "      \"telephone\": \"\",\n" +
                "      \"order\": [\n" +
                "        0\n" +
                "      ],\n" +
                "      \"external_profile\": {\n" +
                "        \"external_attr\": [],\n" +
                "        \"external_corp_name\": \"\"\n" +
                "      },\n" +
                "      \"qr_code\": \"https://open.work.weixin.qq.com/wwopen/userQRCode?vcode=vccbeeb71118e2a03c\",\n" +
                "      \"alias\": \"20140392\",\n" +
                "      \"is_leader_in_dept\": [\n" +
                "        0\n" +
                "      ],\n" +
                "      \"address\": \"\",\n" +
                "      \"thumb_avatar\": \"http://wework.qpic.cn/wwhead/duc2TvpEgST9hicuyypLEKEwxplgFSRsHibBQUuibGIZJPkCCxWaqM2QtDMlTl8TUia0shzS7DB7n50/100\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"userid\": \"1165867687@qq.com\",\n" +
                "      \"name\": \"杨浩\",\n" +
                "      \"department\": [\n" +
                "        129\n" +
                "      ],\n" +
                "      \"position\": \"\",\n" +
                "      \"mobile\": \"18251211660\",\n" +
                "      \"gender\": \"1\",\n" +
                "      \"email\": \"yanghao@khmm.com.cn\",\n" +
                "      \"avatar\": \"http://wework.qpic.cn/bizmail/7ib0KutOrSsFicyKQPyias2oBKlfm1N2jKSGa03Qp33Rr478FP2btpPpA/0\",\n" +
                "      \"status\": 1,\n" +
                "      \"enable\": 1,\n" +
                "      \"isleader\": 0,\n" +
                "      \"extattr\": {\n" +
                "        \"attrs\": []\n" +
                "      },\n" +
                "      \"hide_mobile\": 0,\n" +
                "      \"english_name\": \"201709189\",\n" +
                "      \"telephone\": \"\",\n" +
                "      \"order\": [\n" +
                "        0\n" +
                "      ],\n" +
                "      \"external_profile\": {\n" +
                "        \"external_attr\": [],\n" +
                "        \"external_corp_name\": \"\"\n" +
                "      },\n" +
                "      \"qr_code\": \"https://open.work.weixin.qq.com/wwopen/userQRCode?vcode=vcafebf72eb663ed77\",\n" +
                "      \"alias\": \"201709189\",\n" +
                "      \"is_leader_in_dept\": [\n" +
                "        0\n" +
                "      ],\n" +
                "      \"address\": \"\",\n" +
                "      \"thumb_avatar\": \"http://wework.qpic.cn/bizmail/7ib0KutOrSsFicyKQPyias2oBKlfm1N2jKSGa03Qp33Rr478FP2btpPpA/100\"\n" +
                "    },\n" +
                "    {\n" +
                "      \"userid\": \"Wangqingqi\",\n" +
                "      \"name\": \"王清旗\",\n" +
                "      \"department\": [\n" +
                "        129\n" +
                "      ],\n" +
                "      \"position\": \"\",\n" +
                "      \"mobile\": \"13815096807\",\n" +
                "      \"gender\": \"1\",\n" +
                "      \"email\": \"\",\n" +
                "      \"avatar\": \"http://wework.qpic.cn/bizmail/Cua67xjT1uXHibJIBThMdr8AF2tj9tobTqpJKJdWpP4tV0wkTOiaahBw/0\",\n" +
                "      \"status\": 2,\n" +
                "      \"enable\": 0,\n" +
                "      \"isleader\": 0,\n" +
                "      \"extattr\": {\n" +
                "        \"attrs\": []\n" +
                "      },\n" +
                "      \"hide_mobile\": 0,\n" +
                "      \"english_name\": \"201803787\",\n" +
                "      \"telephone\": \"\",\n" +
                "      \"order\": [\n" +
                "        0\n" +
                "      ],\n" +
                "      \"external_profile\": {\n" +
                "        \"external_attr\": [],\n" +
                "        \"external_corp_name\": \"\"\n" +
                "      },\n" +
                "      \"qr_code\": \"https://open.work.weixin.qq.com/wwopen/userQRCode?vcode=vc7a2f7b409b670237\",\n" +
                "      \"alias\": \"201803787\",\n" +
                "      \"is_leader_in_dept\": [\n" +
                "        0\n" +
                "      ],\n" +
                "      \"address\": \"\",\n" +
                "      \"thumb_avatar\": \"http://wework.qpic.cn/bizmail/Cua67xjT1uXHibJIBThMdr8AF2tj9tobTqpJKJdWpP4tV0wkTOiaahBw/100\"\n" +
                "    }\n" +
                "  ]\n" +
                "}";
        Res o = objectMapper.readValue(json, new TypeReference<Res>() {
        });
        Map<String, Object> stringObjectMap = o.userlist.get(0);
        Set<Map.Entry<String, Object>> entries = stringObjectMap.entrySet();
        StringBuilder sb = new StringBuilder();
        for (Map.Entry<String, Object> entry : entries) {
            String key = entry.getKey();
            Object value = entry.getValue();
            Class<?> clazzStr=String.class;
            Class<?> clazzLong=Long.class;
            Class<?> clazzInteger=Integer.class;
            Class<?> clazzList= ArrayList.class;
            if(value.getClass()==clazzStr) {
                String field = String.format("private %s %s;\n","String" ,key);
                sb.append(field);
            }
            if(value.getClass()==clazzLong||value.getClass()==clazzInteger) {
                String field = String.format("private %s %s;\n","Long" ,key);
                sb.append(field);
            }
            if(value.getClass()==clazzList) {
                String field = String.format("private %s %s;\n","List<Long>" ,key);
                sb.append(field);
            }
        }

        System.out.println(sb.toString());
    }

    @Data
    @ToString
    static class Res {
        int errcode;
        String errmsg;
        List<Map<String, Object>> userlist;
    }

    static ObjectMapper objectMapper = new ObjectMapper();

    private static void test02() {
        String str = "jsapi_ticket=sM4AOVdWfPE4DxkXGEs8VMCPGGVi4C3VM0P37wVUCFvkVAy_90u5h9nbSlYy3-Sl-HhTdfl2fzFy1AOcHKP7qg&noncestr=Wm3WZYTPz0wzccnW&timestamp=1414587457&url=http://mp.weixin.qq.com?params=value";
        Digester digester = new Digester(DigestAlgorithm.SHA1);
        String digestHex = digester.digestHex(str);
        System.out.println(digestHex);
    }

    private static void test01() {
        String template = "jsapi_ticket={0}&" +
                "noncestr={1}&" +
                "timestamp={2}&" +
                "url={3}";
        String format = MessageFormat.format(template, 123, "fff");
        System.out.println(format);
    }

    public void toJson() throws JsonProcessingException {
        String json = null;
        User user = new User();
        user.setTaskIds("123--456--333");
        user.setName("张三");
        AjaxJson<User> a = new AjaxJson<>();
        a.data=user;
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.enable(SerializationFeature.WRAP_ROOT_VALUE);
        json = objectMapper.writeValueAsString(a);
        System.out.println(json);
    }
}
